package com.dd.machine.main.user.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.cn.dd.machine.R;
import com.dd.machine.base.utils.ImageLoaderUtils;
import com.dd.machine.main.user.model.AppGoodsKind;

import java.util.List;

/**
 * Created by Administrator on 2017/7/4.
 */

public class GoodsKindAdapter extends RecyclerView.Adapter<GoodsKindAdapter.MyViewHolder>implements View.OnClickListener{

    private Context mContext;
    private List<AppGoodsKind> mDatas;

    public GoodsKindAdapter(Context mContext, List<AppGoodsKind> mDatas) {
        this.mContext = mContext;
        this.mDatas = mDatas;
    }

    private OnItemClickListener mOnItemClickListener = null;

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取position
            mOnItemClickListener.onItemClick(v,(int)v.getTag());
        }
    }

    //define interface
    public static interface OnItemClickListener {
        void onItemClick(View view , int position);
    }
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.item_kind, parent, false);
        MyViewHolder vh = new MyViewHolder(view);
        view.setOnClickListener(this);
        return new MyViewHolder(view);

    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        final String title=mDatas.get(position).getTypeName();
        String url = mDatas.get(position).getImg();
        holder.tv_title.setText(title.split(",")[0]);
        ImageLoaderUtils.display(url,holder.iv);
        holder.itemView.setTag(position);
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }



    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tv_title;
        ImageView iv;

        public MyViewHolder(View itemView) {
            super(itemView);
            tv_title = (TextView) itemView.findViewById(R.id.kind_name_tv);
            iv = (ImageView) itemView.findViewById(R.id.kind_name_iv);
        }
    }

}
